﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trans.member;

namespace Trans.car
{
    public partial class Manage_car : Form
    {
        public Manage_car()
        {
            InitializeComponent();
        }

        private void Manage_car_Load(object sender, EventArgs e)
        {
           
            this.tb_carTableAdapter.Fill(this.dataSet.tb_car);

        }

        private void lb_driver_Click(object sender, EventArgs e)
        {

        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren())
            {
                try
                {

                    tbcarBindingSource.EndEdit();
                    tb_carTableAdapter.Update(dataSet.tb_car);
                    this.Close();
                    MessageBox.Show("操作成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("出错啦" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            tbcarBindingSource.CancelEdit();
            this.Close();
        }

        private void bt_cartype_Click(object sender, EventArgs e)
        {
            Select_cartype f = new Select_cartype();
            
            if((f.ShowDialog()==DialogResult.OK)&&(f.HasSelected))
            {
                DataRowView row = (DataRowView)tbcarBindingSource.Current;
                row["cartype_id"] = f.Id;
                lb_cartype.Text = f.Model;
            }
        }

        private void bt_driver_Click(object sender, EventArgs e)
        {
            Select_driver f = new Select_driver();
            if((f.ShowDialog()==DialogResult.OK)&&(f.HasSelected))
            {
                DataRowView row = (DataRowView)tbcarBindingSource.Current;
                row["driver_id"]=f.Id;
                lb_driver.Text = f.Name_s;
            }
        }

        private void gv_car_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            tb_cartypeTableAdapter.FillById(dataSet.tb_cartype,(int)gv_car.Rows[e.RowIndex].Cells["c_cartypeid"].Value);
            tb_memberTableAdapter.FillById(dataSet.tb_member,(int)gv_car.Rows[e.RowIndex].Cells["c_driverid"].Value);
            lb_cartype.Text = (string)dataSet.tb_cartype.Rows[0]["model"];
            lb_driver.Text = (string)dataSet.tb_member.Rows[0]["name"];
        }

        private void gv_car_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            session.Color_Const.set_gv_color(gv_car);
        }
    }
}
